/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.controller;

import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;

import ca.usherbrooke.agent.controller.shared.IAgent;
import ca.usherbrooke.agent.controller.shared.IMessage;
import ca.usherbrooke.behaviours.controller.request.MessageBehaviour;
import ca.usherbrooke.mainApp.MainAppAgent;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class BasicAgentLauncher extends BasicAgentController {

	private static final long serialVersionUID = 1L;

	private IAgent agent;
	private MessageBehaviour messageBehaviour;

	private Boolean isEntityInit;

	@Override
	protected void setup() {
		isEntityInit = false;
		super.setup();

		String agentClass = (String) getArguments()[argumentsIdx + 0];
		argumentsIdx += 1;

		// create on the fly the agent
		try {
			URL[] allURls = new URL[1];
			allURls[0] = MainAppAgent.DIRECTORY_AGENT.toURI().toURL();

			// Create a new class loader with the directory
			ClassLoader loader = new URLClassLoader(allURls);

			Class<?> c = loader.loadClass(agentClass);

			Constructor<?>[] constructor = c.getDeclaredConstructors();
			for (Constructor<?> construct : constructor) {
				Class<?>[] allParams = construct.getParameterTypes();

				if (allParams.length == 1) {
					agent = (IAgent) construct.newInstance(this);

					messageBehaviour = new MessageBehaviour(this, agent);
					this.addBehaviour(messageBehaviour);
				}
			}
		} catch (Exception ex) {
			ExceptionTool.showError("An error occur in entity "
					+ getEntityGUID() + ", the agent will be deleted", ex);
			doDelete();
		}

		if (isEntityInit) {
			try {
				agent.initEntity();
			} catch (Exception ex) {
				// if the user as an exception
				ExceptionTool.showError("An error occur in entity "
						+ getEntityGUID() + ", when initEntity ", ex);
			}
		}
	}

	@Override
	public void initEntity() {
		super.initEntity();
		isEntityInit = true;

		if (agent != null) {
			try {
				agent.initEntity();
			} catch (Exception ex) {
				// if the user as an exception
				ExceptionTool.showError("An error occur in entity "
						+ getEntityGUID() + ", when initEntity ", ex);
			}
		}
	}

	@Override
	public void setWorld(IWorldModel world) {
		super.setWorld(world);

		if (isEntityInit) {
			try {
				this.agent.worldChanged(world);
			} catch (Exception ex) {
				// if the user as an exception
				ExceptionTool.showError("An error occur in entity "
						+ getEntityGUID() + ", when worldChanged ", ex);
			}
		}
	}

	public void sendMessage(Long entityGuid, IMessage msg) {
		messageBehaviour.sendMessage(entityGuid, msg);
	}

	@Override
	public void doDelete() {
		super.doDelete();
		this.agent.entityDied();
	}

}
